/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.URI;
import lastplaylist.LastPlaylistView;

/**
 *
 * @author FoSa
 */
public class Options implements Serializable {

    private String apikey = "b25b959554ed76058ac220b7b2e0a026";
    private String secret = "123456789012345678901234567890--";

    public Options() {
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(lastplaylist.LastPlaylistApp.class).getContext().getResourceMap(LastPlaylistView.class);
        setApikey(resourceMap.getString("apikey"));
        setSecret(resourceMap.getString("secret"));
        salvarOpções();
    }

    public void carregarOpções() {
        carregarOpções(this);
    }

    public void salvarOpções() {
        salvarOpções(this);
    }

    /**
     * @return the apikey
     */
    public String getApikey() {
        return apikey;
    }

    /**
     * @param apikey the apikey to set
     */
    public void setApikey(String apikey) {
        this.apikey = apikey;
    }

    /**
     * @return the secret
     */
    public String getSecret() {
        return secret;
    }

    /**
     * @param secret the secret to set
     */
    public void setSecret(String secret) {
        this.secret = secret;
    }

    public static void carregarOpções(Options op) {
        try {
            URI uri = new URI(local() + "/config.ini");
            File file = new File(uri);
            FileInputStream filein = new FileInputStream(file);
            ObjectInputStream input = new ObjectInputStream(filein);
            op = (Options) input.readObject();
            input.close();
        } catch (Exception e) {
            op = new Options();
        }
    }

    public static void salvarOpções(Options op) {
        try {
            URI uri = new URI(local() + "/config.ini");
            File file = new File(uri);
            file.createNewFile();
            FileOutputStream fileout = new FileOutputStream(file);
            ObjectOutputStream output = new ObjectOutputStream(fileout);
            output.writeObject(op);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String local() {
        String localret = Options.class.getName();
        localret = Options.class.getResource("/" + localret.replace('.', '/') + ".class").toString();

        if (localret.startsWith("jar:")) {
            localret = localret.substring(4);
            localret = localret.substring(0, localret.lastIndexOf(".jar"));
            localret = localret.substring(0, localret.lastIndexOf("/") + 1);

        }

        localret = localret.substring(0, localret.lastIndexOf('/'/*System.getProperty("file.separator")*/) + 1);
        return localret;
    }
}
